package com.ykdl.member.kid.advert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.ActivitiesAdvertListBean;
import com.ykdl.member.kid.beans.AdvertBean;
import com.ykdl.member.kid.beans.AdvertListBean;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.beans.DoctorOnlineListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class AdvertListActivity extends BaseScreen{
	private RefreshListView  advert_list ;
	private AdvertAdapter adapter ;
	private BitmapUtils bitmapUtil ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("活动", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.advertlistactivity_xml);
		findViewID();
	}
	public void findViewID(){
		bitmapUtil = new BitmapUtils(this);
		advert_list  = (RefreshListView)findViewById(R.id.advert_list);
		advert_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				AdvertBean bean= (AdvertBean)adapter.getItem(arg2);
				goAction(bean.getUrl());
			}
		});
		adapter = new AdvertAdapter();
		inintdata();
	}
	public void inintdata() {
		showProgress("正在发送请稍等...");
		String  pageurl =KidConfig.GET_ADVERTLIST;
		Map<String,String> map = new HashMap<String, String>();
		map.put("keys", "activities");
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getAdvertListTag(),  ActivitiesAdvertListBean.class);
	}
	
	class getAdvertListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ActivitiesAdvertListBean) {
				ActivitiesAdvertListBean bean = (ActivitiesAdvertListBean)t;
				if (bean.getDiscovery_top_ad()!=null) {
					adapter.setList(bean.getDiscovery_top_ad());
					advert_list.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					Toast.makeText(mContext, "成功", 1).show();
					advert_list.setMoreButtoIsGon(true);
					hidErrorTip( );
				}else {
					showErrorTip("没有数据...", "");
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	class AdvertAdapter extends BaseAdapter{
		private ArrayList<AdvertBean>  list;
		public void setList(ArrayList<AdvertBean>  list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list != null) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			if (getCount() != 0) {
				return list.get(position-1);
			} else {
				return null;
			}
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			HolderView holderView;
			if (convertView == null) {
				holderView = new HolderView();
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.advertadapter_item, null);
				
				holderView.advert_image = (ImageView) convertView.findViewById(R.id.advert_image);
				convertView.setTag(holderView);
				}else {
					holderView = (HolderView) convertView.getTag();
				}
			if (list.get(position).getFilemeta()!=null) {
				String url = list.get(position).getFilemeta().getDownload_urls().getOrigin().getUrl();
				bitmapUtil.display(holderView.advert_image, url);
			}
			return convertView;
		}
		class HolderView{
			ImageView advert_image ;
		}
		
	}

}
